package com.ruoyi.aitcommon.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.utils.DBUtils;
import com.ruoyi.aitcommon.utils.JSONResult;
import com.ruoyi.aitcommon.utils.RuntimeManager;
import com.ruoyi.aitcommon.utils.SQLUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.aitcommon.domain.AitEntityInfo;
import com.ruoyi.aitcommon.service.IAitEntityInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 接口实体记录Controller
 * 
 * @author cli
 * @date 2022-02-15
 */
@RestController
@RequestMapping("/aitcommon/EntityInfo")
public class AitEntityInfoController extends BaseController
{
    @Autowired
    private IAitEntityInfoService aitEntityInfoService;
    @Autowired
    private RuntimeManager runtimeManager;

    @GetMapping("/errorClear")
    public JSONObject errorClear()
    {
        try{
            if(runtimeManager.isFLowRun())
                throw new Exception("有流程在运行,请系统空闲时执行该功能");

            DBUtils db=new DBUtils();
            SQLUtils.execProcUpdate("{ call p_aiterror } ",db);
            return JSONResult.success();
        }catch (Exception ex){
            return JSONResult.error(ex.getMessage());
        }
    }

    /**
     * 查询接口实体记录列表
     */
    @PreAuthorize("@ss.hasPermi('aitcommon:EntityInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AitEntityInfo aitEntityInfo)
    {
        startPage();
        List<AitEntityInfo> list = aitEntityInfoService.selectAitEntityInfoList(aitEntityInfo);
        return getDataTable(list);
    }

    /**
     * 导出接口实体记录列表
     */
    @PreAuthorize("@ss.hasPermi('aitcommon:EntityInfo:export')")
    @Log(title = "接口实体记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AitEntityInfo aitEntityInfo)
    {
        List<AitEntityInfo> list = aitEntityInfoService.selectAitEntityInfoList(aitEntityInfo);
        ExcelUtil<AitEntityInfo> util = new ExcelUtil<AitEntityInfo>(AitEntityInfo.class);
        return util.exportExcel(list, "接口实体记录数据");
    }

    /**
     * 获取接口实体记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('aitcommon:EntityInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(aitEntityInfoService.selectAitEntityInfoById(id));
    }

    /**
     * 新增接口实体记录
     */
    @PreAuthorize("@ss.hasPermi('aitcommon:EntityInfo:add')")
    @Log(title = "接口实体记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AitEntityInfo aitEntityInfo)
    {
        return toAjax(aitEntityInfoService.insertAitEntityInfo(aitEntityInfo));
    }

    /**
     * 修改接口实体记录
     */
    @PreAuthorize("@ss.hasPermi('aitcommon:EntityInfo:edit')")
    @Log(title = "接口实体记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AitEntityInfo aitEntityInfo)
    {
        return toAjax(aitEntityInfoService.updateAitEntityInfo(aitEntityInfo));
    }

    /**
     * 删除接口实体记录
     */
    @PreAuthorize("@ss.hasPermi('aitcommon:EntityInfo:remove')")
    @Log(title = "接口实体记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aitEntityInfoService.deleteAitEntityInfoByIds(ids));
    }
}
